package com.thiswhyme.stream.rabbit.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;

/**
 * @description: @GlobalChannelInterceptor demo
 * @author: thiswhyme
 * @date: 2021/2/5
 */
@Slf4j
public class BizMessageChannelInterceptor implements ChannelInterceptor {

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        log.info("===========preSend: " + message.toString());
        log.info("===========preSend: " + channel.toString());
        return message;
    }

    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        log.info("===========postSend: " + message.toString());
        log.info("===========postSend: " + channel.toString());
        log.info("===========postSend: " + sent);
    }

    @Override
    public void afterSendCompletion(
            Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex) {
        log.info("===========afterSendCompletion: " + message.toString());
        log.info("===========afterSendCompletion: " + channel.toString());
        log.info("===========afterSendCompletion: " + sent);
    }

    @Override
    public boolean preReceive(MessageChannel channel) {
        log.info("===========preReceive: " + channel.toString());
        return true;
    }

    @Override
    public Message<?> postReceive(Message<?> message, MessageChannel channel) {
        log.info("===========postReceive: " + message.toString());
        log.info("===========postReceive: " + channel.toString());
        return message;
    }

    @Override
    public void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel,
                                       @Nullable Exception ex) {
        log.info("===========afterReceiveCompletion: " + message.toString());
        log.info("===========afterReceiveCompletion: " + channel.toString());
    }
}
